package view;

import entity.Admin;
import entity.CounterStaff;
import entity.Manager;
import entity.User;
import service.UserServiceImpl;
import util.DateUtil;
import util.LogUtil;

import java.util.Date;
import java.util.List;
import java.util.Scanner;

public class AdminView {
    private Scanner scanner = new Scanner(System.in);
    private UserServiceImpl userService = new UserServiceImpl();
    private User currentUser;

    public void showAdminView(User user) {
        currentUser = user;
        while (true) { // 内层循环，确保退出登录后可以返回登录界面
            System.out.println("===== 管理员界面 =====");
            System.out.println("1. 用户管理");
            System.out.println("2. 修改密码");
            System.out.println("3. 退出登录");
            System.out.print("请选择操作：");
            String choice = scanner.nextLine();

            switch (choice) {
                case "1":
                    showUserManagementMenu();
                    break;
                case "2":
                    modifyPassword();
                    break;
                case "3":
                    System.out.println("退出登录！");
                    return; // 返回到登录界面
                default:
                    System.out.println("无效的选择，请重新输入！");
            }
        }
    }

    private void showUserManagementMenu() {
        while (true) {
            System.out.println("===== 用户管理 =====");
            System.out.println("1. 添加用户");
            System.out.println("2. 删除用户");
            System.out.println("3. 修改用户");
            System.out.println("4. 查询用户");
            System.out.println("5. 列出所有用户");
            System.out.println("6. 返回上一级");
            System.out.print("请选择操作：");
            String choice = scanner.nextLine();

            switch (choice) {
                case "1":
                    addUser();
                    break;
                case "2":
                    deleteUser();
                    break;
                case "3":
                    modifyUser();
                    break;
                case "4":
                    searchUser();
                    break;
                case "5":
                    listAllUsers();
                    break;
                case "6":
                    return;
                default:
                    System.out.println("无效的选择，请重新输入！");
            }
        }
    }

    private void addUser() {
        System.out.print("请输入用户 ID：");
        String userId = scanner.nextLine();
        System.out.print("请输入用户名：");
        String username = scanner.nextLine();
        System.out.print("请输入用户类型（admin/manager/counterStaff）：");
        String userType = scanner.nextLine();
        System.out.print("请输入手机号：");
        String phone = scanner.nextLine();

        entity.User user = null;
        switch (userType) {
            case "admin":
                user = new Admin(userId, username, "123456", new Date(), phone);
                break;
            case "manager":
                user = new Manager(userId, username, "123456", new Date(), phone);
                break;
            case "counterStaff":
                user = new CounterStaff(userId, username, "123456", new Date(), phone);
                break;
            default:
                System.out.println("无效的用户类型！");
                return;
        }

        boolean result = userService.addUser(user);
        if (result) {
            LogUtil.log("INFO", "添加用户成功，用户 ID：" + userId, currentUser.getUsername());
            System.out.println("添加用户成功！");
        } else {
            System.out.println("添加用户失败！");
        }
    }

    private void deleteUser() {
        System.out.print("请输入要删除的用户 ID：");
        String userId = scanner.nextLine();

        boolean result = userService.deleteUser(userId);
        if (result) {
            LogUtil.log("INFO", "删除用户成功，用户 ID：" + userId, currentUser.getUsername());
            System.out.println("删除用户成功！");
        } else {
            System.out.println("删除用户失败！");
        }
    }

    private void modifyUser() {
        System.out.print("请输入要修改的用户 ID：");
        String userId = scanner.nextLine();

        User user = userService.getUserById(userId);
        if (user == null) {
            System.out.println("用户不存在！");
            return;
        }

        System.out.print("请输入新的用户名（留空则不修改）：");
        String username = scanner.nextLine();
        if (!username.isEmpty()) {
            user.setUsername(username);
        }

        System.out.print("请输入新的手机号（留空则不修改）：");
        String phone = scanner.nextLine();
        if (!phone.isEmpty()) {
            user.setPhone(phone);
        }

        System.out.print("请输入新的用户类型（留空则不修改）：");
        String userType = scanner.nextLine();
        if (!userType.isEmpty()) {
            switch (userType) {
                case "manager":
                    if (!(user instanceof Manager)) {
                        user.setUserType("manager");
                    }
                    break;
                case "counterStaff":
                    if (!(user instanceof CounterStaff)) {
                        user.setUserType("counterStaff");
                    }
                    break;
                default:
                    System.out.println("无效的用户类型！");
                    return;
            }
        }

        boolean result = userService.updateUser(user);
        if (result) {
            LogUtil.log("INFO", "修改用户成功，用户 ID：" + userId, currentUser.getUsername());
            System.out.println("修改用户成功！");
        } else {
            System.out.println("修改用户失败！");
        }
    }

    private void searchUser() {
        System.out.print("请输入用户 ID 或用户名（留空则查询所有用户）：");
        String keyword = scanner.nextLine();

        List<User> users = userService.getAllUsers();
        System.out.println("===== 用户列表 =====");
        System.out.println("用户 ID\t\t用户名\t\t注册时间\t\t用户类型\t\t手机号");
        for (User user : users) {
            if (keyword.isEmpty() || user.getUserId().contains(keyword) || user.getUsername().contains(keyword)) {
                System.out.println(user.getUserId() + "\t" + user.getUsername() + "\t" +
                        DateUtil.formatDate(user.getRegisterTime()) + "\t" +
                        user.getUserType() + "\t" + user.getPhone());
            }
        }
    }

    private void listAllUsers() {
        List<User> users = userService.getAllUsers();
        System.out.println("===== 用户列表 =====");
        System.out.println("用户 ID\t\t用户名\t\t注册时间\t\t用户类型\t\t手机号");
        for (User user : users) {
            System.out.println(user.getUserId() + "\t" + user.getUsername() + "\t" +
                    DateUtil.formatDate(user.getRegisterTime()) + "\t" +
                    user.getUserType() + "\t" + user.getPhone());
        }
    }

    private void modifyPassword() {
        System.out.print("请输入旧密码：");
        String oldPassword = scanner.nextLine();
        System.out.print("请输入新密码：");
        String newPassword = scanner.nextLine();
    }
}
